<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="/layui/css/layui.css">
  <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<div class="panel panel-default operation" hidden>
  <div class="panel-heading title"></div>
<div class="layui-card-body">
<form class="layui-form " action="" lay-filter="info" style="margin-top: 10px">
    
    <div class="layui-form-item" hidden>
      <label class="layui-form-label">加油站id</label>
      <div class="layui-input-block">
        <input type="jcxxId" name="jcxxId" placeholder="请输入加油站id" autocomplete="off" class="layui-input">
      </div>
    </div>
    
        <div class="layui-form-item">
      <label class="layui-form-label">储油罐名称</label>
      <div class="layui-input-block">
        <input type="cygMc" name="cygMc" placeholder="请输入储油罐名称" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">油品</label>
      <div class="layui-input-block">
        <input type="yp" name="yp" placeholder="请输入油品" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">储油罐容积</label>
      <div class="layui-input-block">
        <input type="cygrj" name="cygrj" placeholder="请输入储油罐容积" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">剩余库存油量</label>
      <div class="layui-input-block">
        <input type="sykcyl" name="sykcyl" placeholder="请输入剩余库存油量" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">油高</label>
      <div class="layui-input-block">
        <input type="yg" name="yg" placeholder="请输入油高" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">水高</label>
      <div class="layui-input-block">
        <input type="sg" name="sg" placeholder="请输入水高" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">温度</label>
      <div class="layui-input-block">
        <input type="wd" name="wd" placeholder="请输入温度" autocomplete="off" class="layui-input">
      </div>
    </div>
        <!-- <div class="layui-form-item">
      <label class="layui-form-label">更新时间</label>
      <div class="layui-input-block">
        <input type="updateTime" name="updateTime" placeholder="请输入更新时间" autocomplete="off" class="layui-input">
      </div>
    </div> -->
    
        <input name="id" hidden/>
  	<div class="layui-form-item">
	  <div class="layui-input-block">
	    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
	    <button  class="layui-btn layui-btn-primary btn_cancel">返回</button>
	  </div>
	</div>
	
</form>
</div>
</div>

<div class="table_div1">
  <div id="searchParam">
    <div class="layui-form-item">
      <div class="layui-input-inline">
        <input type="text" id="nameInput" class="layui-input"  autocomplete="off" placeholder="请输入加油站名称">
      </div>

      <div class="layui-input-inline ">
        <button class="layui-btn" onclick="search1()" id="search">查询</button>
      </div>
      <div class="layui-input-inline "> 
        <button class="layui-btn" onclick="exportTemplate()">导出模板</button>
      </div>
      <div class="layui-input-inline ">
        <button class="layui-btn" id="importData">导入数据</button>
      </div>
    </div>

  </div>
  <table class="layui-table" id="showTable1" lay-filter="showTable1" ></table>
</div>


<div class="table_div2" hidden>
  <div id="searchParam">
    <div class="layui-form-item">
      <div class="layui-input-inline">
        <button class="layui-btn layui-btn-primary btn_cancel">返回</button>
      </div>
    </div>
  </div>
  <table class="layui-table" id="showTable2" lay-filter="showTable2" ></table>
</div>




<script type="text/html" id="tool1">
  <a class="layui-btn layui-btn-xs" lay-event="add">添加-加油罐信息</a>
  <a class="layui-btn layui-btn-xs" lay-event="view">查看-加油罐信息</a>
</script>
<script type="text/html" id="tool2">
  <a class="layui-btn layui-btn-xs" lay-event="delete">删除</a>
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>

</body>
</html>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
  //获取token
  var token = CoreUtil.getData("access_token");
  //地址栏转义token中的#号
  var tokenQuery = token.replace("#", "%23");
  var tableIns1;
  var tableIns2;
  
  var table = layui.table;
  var form = layui.form;
  var layer = layui.layer;
  var $ = jQuery = layui.jquery;
  var laydate = layui.laydate;
  var upload = layui.upload;

  layui.use(['table', 'layer', 'laydate', 'upload'], function () {

		upload.render({
			elem: '#importData' //绑定元素
			,url: '/gas/cygxx/importData?authorization='+tokenQuery //上传接口
			,exts: 'xlsx' //限制上传文件
			,size:"204800"
			,before: function(obj){
				layer.msg('文件上传中...', {
				icon: 16,
				shade: 0.01,
				time: 0
				})
			}
			,done: function(res){
				if(res.code == "0"){
					layer.msg("导入文件上传成功！");
					search();
				}else{
					layer.msg(res.msg);
				}
		      }
		      ,error: function(){
		    	  layer.msg("文件上传失败，请稍后重试！");
		      }
		  	});
	  
	  
	  
	    //加载table
	    tableIns1 = table.render({
	      elem: '#showTable1'
	      , contentType: 'application/json'
	      , headers: {"authorization": token}
	      , page: true //开启分页
	      , url: '/gas/stationinfo/list1' //数据接口
	      , method: 'POST'
	      , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
	        return {
	          "code": res.code, //解析接口状态
	          "msg": res.msg, //解析提示文本
	          "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
	          "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
	        }
	      }
	      , cols: [
	        [
	          {type: 'checkbox', fixed: 'left'},
	          {field: 'id', title: '主键', sort: true, hide: true},
	          {field: 'name', title: '加油站名称', width: 520,sort: true},
	          {field: 'taxorgName', title: '所属机关', width: 400,sort: true}, 
	          
			  {width: 280, toolbar: "#tool1", title: '操作',fixed: 'right'},
	        ]
	      ]
	      , toolbar: '#toolbar'
	    });

	    
	    //加载table
	    tableIns2 = table.render({
	      elem: '#showTable2'
	      , contentType: 'application/json'
	      , headers: {"authorization": token}
	      , page: true //开启分页
	      , url: '/gas/cygxx/list' //数据接口
	      , method: 'POST'
	      , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
	        return {
	          "code": res.code, //解析接口状态
	          "msg": res.msg, //解析提示文本
	          "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
	          "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
	        }
	      }
	      , cols: [
	        [
				{type: 'checkbox', fixed: 'left'},
				{field: 'jcxxId', title: '加油站id', sort: true},
				{field: 'cygMc', title: '储油罐名称', sort: true},
				{field: 'yp', title: '油品', sort: true},
				{field: 'cygrj', title: '储油罐容积', sort: true},
				{field: 'sykcyl', title: '剩余库存油量', sort: true},
				{field: 'yg', title: '油高', sort: true},
				{field: 'sg', title: '水高', sort: true},
				{field: 'wd', title: '温度', sort: true},
				{field: 'updateTime', title: '更新时间', sort: true},
				{field: 'id', title: '主键', sort: true},
				{width: 120, toolbar: "#tool2", title: '操作'}
	        ]
	      ]
	      , toolbar: '#toolbar'
	    });
	    
    //列操作
    table.on('tool(showTable1)',function (obj) {
      var data = obj.data;
      switch (obj.event) {
        case 'add':
        	$(".table_div1").hide();
        	$(".table_div2").hide();
            $(".operation").show();
            $(".title").html("新增");
            $(".operation input[name=jcxxId]").val(data.id);
            $(".operation input[name=cygMc]").val("");
            $(".operation input[name=yp]").val("");
            $(".operation input[name=cygrj]").val("");
            $(".operation input[name=sykcyl]").val("");
            $(".operation input[name=yg]").val("");
            $(".operation input[name=sg]").val("");
            $(".operation input[name=wd]").val("");
            $(".operation input[name=updateTime]").val("");
            $(".operation input[name=id]").val("");
          break;
        case 'view':
        	$(".table_div1").hide();
        	$(".table_div2").show();
            $(".operation").hide();
            
        	search2(data.id);
          break;
      }
    });

    table.on('tool(showTable2)',function (obj) {
        var data = obj.data;
        switch (obj.event) {
          case 'delete':
	          var ids=[];
	          ids.push(data.id);
	          tipDialog(ids,data.jcxxId);
	          
            break;
          case 'edit':
        	  $(".table_div1").hide();
        	  $(".table_div2").hide();
        	  
              $(".operation").show();
              $(".title").html("编辑");
              $(".operation input[name=jcxxId]").val(data.jcxxId);
              $(".operation input[name=cygMc]").val(data.cygMc);
              $(".operation input[name=yp]").val(data.yp);
              $(".operation input[name=cygrj]").val(data.cygrj);
              $(".operation input[name=sykcyl]").val(data.sykcyl);
              $(".operation input[name=yg]").val(data.yg);
              $(".operation input[name=sg]").val(data.sg);
              $(".operation input[name=wd]").val(data.wd);
              $(".operation input[name=updateTime]").val(data.updateTime);
              $(".operation input[name=id]").val(data.id);
              
            break;
        }
      });
    
    //删除
    var tipDialog=function (ids,jcxxid) {
      layer.open({
        content: "确定要删除么?",
        yes: function(index, layero){
          layer.close(index); //如果设定了yes回调，需进行手工关闭
          CoreUtil.sendDelete("/gas/cygxx/delete",ids,function (res) {
            layer.msg(res.msg, {time:1000},function () {
              search2(jcxxid);
            });
          });
        }
      });
    };

    //返回
    $(".btn_cancel").click(function() {
      $(".table_div1").show();
      $(".table_div2").hide();
      
      $(".operation").hide();
      return false;
    });

    //监听保存
    form.on('submit(submit)', function(data){
      if(data.field.id===undefined || data.field.id===null || data.field.id===""){
        CoreUtil.sendPost("/gas/cygxx/add",data.field,function (res) {
          $(".table_div1").show();
          $(".table_div2").hide();
          $(".operation").hide();
          search1();
          
        });
      }else {
        CoreUtil.sendPut("/gas/cygxx/update",data.field,function (res) {
        	$(".table_div1").hide();
            $(".table_div2").show();
            $(".operation").hide();
          	search2(data.field.jcxxId);
        });
      }

      return false;
    });
  });

  //执行查询
  function search1() {
    //这里以搜索为例
    tableIns1.reload({
      where: { //设定异步数据接口的额外参数，任意设
    	  name: $("#nameInput").val()
      }
      , page: {
        curr: 1 //重新从第 1 页开始
      }
    });
  };
  
  function search2(id) {
	    //这里以搜索为例
	    tableIns2.reload({
	      where: { //设定异步数据接口的额外参数，任意设
	    	  jcxxId: id
	      }
	      , page: {
	        curr: 1 //重新从第 1 页开始
	      }
	    });
	};
  
   function exportTemplate(){
		  var map = {
				name: $("#nameInput").val()
		  }
		  
		  CoreUtil.sendPost("/gas/cygxx/exportTemplate",map,function (res) {
			  if(res.code == 0){
				var file = res.data[2];
				window.open(file,'_blank');
			  }else{
				layer.msg(res.msg);
				return false;
			  }
	          
		  });
   }
	
   function importData(){
		
   }
	
</script>